package com.example.medical.controller.user;

import com.example.medical.entity.dto.LoginDto;
import com.example.medical.entity.dto.SaveUserDto;
import com.example.medical.entity.dto.UpdatePasswordDto;
import com.example.medical.entity.pojo.User;
import com.example.medical.service.UserService;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/user_login")
    public Result UserLogin(@RequestBody LoginDto dto){
        return userService.UserLogin(dto);
    }

    @PostMapping("/save_user")
    public Result SaveUser(@RequestBody SaveUserDto dto){
        return userService.SaveUser(dto);
    }

    @PostMapping("/update_user_password")
    public Result UpdateUserPassword(@RequestBody UpdatePasswordDto dto){
        return userService.UpdateUserPassword(dto);
    }

    @GetMapping("/get_user")
    public Result GetUser(){
        User getUser = userService.getById(BaseContext.getCurrentId());
        return Result.okResult(getUser);
    }

    @PostMapping("/update_user_detail")
    public Result UpdateUserDetail(@RequestParam("file")MultipartFile file,
                                   @RequestParam(value = "divisionId",required = false) Long divisionId,
                                   @RequestParam(value = "sex",required = false) Integer sex) throws Exception {
        return userService.UpdateUserDetail(file,divisionId,sex);
    }

    @GetMapping("/user_logout")
    public Result UserLogout(){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        BaseContext.removeCurrentId();
        return Result.okResult(Code.SUCCESS);
    }

}
